package com.woniu.service.Impl.homePage;

import com.woniu.service.homePage.ISearchHistoryService;
import com.woniu.util.ResData;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author 凌雨
 * @data 2023/3/9 14:53
 */
@Service
public class SearchHistoryServiceImpl implements ISearchHistoryService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 查询搜索历史
     * @param phone 用户手机号
     * @return
     */
    @Override
    public ResData<?> querySearchHistory(String phone) {
        if (phone == null){
            return new ResData<>().ok();
        }
        SetOperations<String, String> opsForSet = redisTemplate.opsForSet();
        Set<String> set = opsForSet.members("search:"+phone);
        if (set == null){
            return new ResData<>().ok();
        }
        return new ResData<>().ok(set);
    }

    /**
     * 删除搜索历史
     * @param phone 用户的手机号
     * @return
     */
    @Override
    public ResData<?> deleteSearchHistory(String phone) {
        redisTemplate.delete("search:"+phone);
        return new ResData<>().ok();
    }
}
